<?php

namespace App\Http\Common\pools\redis;

use Swoole\ConnectionPool;
use Swoole\Database\MysqliConfig;
use Swoole\Database\MysqliPool;
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;

class MysqliConfigPool extends ConnectionPool
{

    protected $connectConfig;

    protected $mysqlInstance;

    public function __construct()
    {
        $this->connectConfig = config('database.connections.db_v2');
    }

    public function connection()
    {
        if (empty($this->mysqlInstance)) {
            $this->mysqlInstance = new PDOPool((new PDOConfig())
                ->withHost($this->connectConfig['host'])
                ->withPort($this->connectConfig['port'])
                // ->withUnixSocket('/tmp/mysql.sock')
                ->withDbName($this->connectConfig['database'])
                ->withCharset('utf8mb4')
                ->withUsername($this->connectConfig['username'])
                ->withPassword($this->connectConfig['password'])
            );
        }
        return $this->mysqlInstance;
    }
}
